package cn.czu.wyw.controller;

import cn.czu.wyw.annocation.LogAnnotation;
import cn.czu.wyw.utils.RabbitMqClientUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author swg.
 * @Date 2020/4/8 22:12
 * @CONTACT 317758022@qq.com
 * @DESC
 */
@Api(value = "Rabbit MQ Demo",tags = "Rabbit MQ测试")
@RestController
@RequestMapping("/rabbit_mq")
public class RabbitMqController {
    //交换器名称
    final String EXCHANGE = "exchange.simple";
    //队列名称
    final String QUEUE = "queue.simple";
    //路由键
    final String ROUTING_KEY = "routing.key.simple";

    @GetMapping("/procedure/{message}")
    public void procedureMessage(@PathVariable(value = "message" ,required = false) String message) throws IOException, TimeoutException, InterruptedException {
        //创建链接
        Connection connection= RabbitMqClientUtil.getConnection();

        //创建信道
        Channel channel = connection.createChannel();

        //声明一个类型为“direct”，持久化，非自动删除的交换器
        channel.exchangeDeclare(EXCHANGE, "direct", true, false, null);

        //声明一个持久化的，排他的，非自动删除的队列
        channel.queueDeclare(QUEUE,true,false,false,null);

        //绑定交换器和队列
        channel.queueBind(QUEUE, EXCHANGE,  ROUTING_KEY);

        //消息
//      message = "This is the simple message!";
        //发送消息
        channel.basicPublish(EXCHANGE, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());

        System.out.println("Send the message: " + message);

        Thread.sleep(10000);
        //关闭信道
        channel.close();
        //关闭连接
        connection.close();
    }
    @GetMapping("/consumer")
    public void consumerMessage() throws IOException, TimeoutException, InterruptedException {

    }

}
